//
//  FamilyDetailVC.m
//  AILock
//
//  Created by 谭亚明 on 2020/6/29.
//  Copyright © 2020 tym. All rights reserved.
//

#import "FamilyDetailVC.h"
#import "UserTool.h"
#import "FamilyManageVC.h"
#import "FamilyManageVC.h"
#import "UIImageView+YPImage.h"
#import "ShowAlertView.h"
#import "UserModel.h"
#import "UIButton+Gradient.h"
#import "FamilyService.h"
@interface FamilyDetailVC ()

@property(nonatomic,weak) IBOutlet  UIImageView *headImage;
@property(nonatomic,weak) IBOutlet UILabel *nameLab;
@property(nonatomic,weak) IBOutlet UILabel *descLab;
@property(nonatomic,weak) IBOutlet UIButton *deteleBtn;
@property(nonatomic,weak) IBOutlet UIButton *joinBtn;
@property(nonatomic,strong) FamilyService *service;
@end

@implementation FamilyDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addBackBtnFunc];
    [self addTitle:@"家庭详情"];
    
    self.deteleBtn.layer.borderColor = rgba(151, 151, 151).CGColor;
    self.deteleBtn.layer.borderWidth = 1;
    [self.joinBtn gradientButtonWithSize:CGSizeMake(300, 44) colorArray:@[(id)rgba(139, 175, 255),(id)rgba(76, 132, 255)] percentageArray:@[@(0.5),@(1)] gradientType:GradientFromLeftTopToRightBottom];
    self.joinBtn.layer.cornerRadius = 5;
    self.joinBtn.layer.masksToBounds = YES;
    
    self.headImage.layer.masksToBounds = YES;
    self.headImage.layer.cornerRadius = self.headImage.frame.size.height/2;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    _nameLab.text = _familyModel.familyName;
    _descLab.text = _familyModel.familyDesc;
    [_headImage yp_setImageUlr:[_familyModel getFamileHeadImage]];
}

-(FamilyService*)service{
    if (_service == nil) {
        _service = [[FamilyService alloc] init];
    }
    return _service;
}

-(void)deleteFamilyWeb{
    TYMWeak(self);
    [self.service getExitfamilyWithFamilyID:self.familyModel.familyId Completion:^(LMJBaseResponse *responsed) {
        if (responsed.code == 0) {
            [WHToast showMessage:@"删除成功" duration:2 finishHandler:^{
                [weakself.navigationController popViewControllerAnimated:YES];
            }];
        }
    }];
}

-(IBAction)doDeleteFamilyAction:(id)sender{
    TYMWeak(self);
    [ShowAlertView showAlertViewTitle:@"提示" message:@"确定删除并退出该家庭吗?" actionBtnTitle:@"确定"
                           hiddenCancel:NO
                           completion:^(NSInteger actionType) {
        if (actionType) {
            [weakself deleteFamilyWeb];
        }
    }];
}

-(IBAction)doJodinFamilyAction:(id)sender{
    [self.service getSwitchoverFamilyWithFamilyID:self.familyModel.familyId Completion:^(LMJBaseResponse * responsed) {
        if (responsed.code == 0) {
            [UserTool setFamily:self.familyModel completion:^(BOOL state) {
                for(UIViewController *vc in  self.navigationController.viewControllers){
                    if([vc isKindOfClass:[FamilyManageVC  class]]){
                        [self.navigationController popToViewController:vc animated:YES];
                    }else{
                        [self.navigationController popToRootViewControllerAnimated:YES];
                    }
                }
            }];
        }
    }];
}


@end
